<script>
import {mapActions} from "vuex";

export default {
  name: "Header",
  data() {
    return {
      keyword: ""
    }
  },
  methods: {
    ...mapActions("userStore", ["userLogout"]),
    /* 点击搜索按钮的回调函数，需要携带参数跳转到search页面 */
    onSearch() {
      // 需要跳转的路由地址
      const location = {name: "search", params: {keyword: this.keyword || undefined}}
      // query参数合并
      location.query = this.$route.query
      // 携带query参数和params参数跳转路由
      this.$router.push(location)
      /*
      * 一：路由的传参分别有三种写法：字符串形式、模板字符串形式、对象形式
      * 第一种字符串的形式:
      *     this.$router.push("/search/" + this.keyword + "?k=" + this.keyword)
      * 第二种模板字符串的形式:
      *     this.$router.push(`/search/${this.keyword}?k=${this.keyword}`)
      * 第三种模对象的形式:
      *     this.$router.push({name: "search", params: {keyword: this.keyword}, query: {k: this.keyword}})
      *
      *
      * 二：路由传参的面试题：
      * 1.路由传递参数(对象写法)，path是否可以结合params参数一起使用?
      *  path结合name使用是错误的用法 this.$router.push({path: "/search", params: {keyword: this.keyword}, query: {k: this.keyword}})
      *  name结合合params才是正确的用法 this.$router.push({name: "search", params: {keyword: this.keyword}, query: {k: this.keyword}})
      *  答：params参数不可以结合path使用，路由跳转传参的时候(对象写法)params参数只能结合命名路由一起使用
      *
      * 2.如何指定params参数可传可不传?
      *  如果指定了params参数占位符，但是路由跳转的时候不传递，此时路径会出现问题 http://localhost:8080/#/?k=%E5%8D%8E%E4%B8%BA
      *  如果需要指定params参数可传可不传需要在params参数占位符后面加个?号 此时路径正常 http://localhost:8080/#/search?k=1
      *  答：如果需要指定params参数可传可不传需要在params参数占位符后面加个?号
      *
      * 3.params参数可传可不传，如果传递的是空字符串如何解决?
      *   答:使用undefined解决
      *   this.$router.push({name: "search", params: {keyword: "" || undefined}, query: {k: this.keyword}})
      *
      * 4.路由组件能不能传递pops?
      *   答：路由组件能传递props，而且有三种写法：布尔值写法、对象写法、函数写法
      * */
    },
    /* 点击退出的回调函数 */
    async logoutHandle() {
      const confirm = await this.$confirm('退出登录, 是否继续?', '提示', {confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning', lockScroll: false}).catch(e => e)
      if (confirm === "cancel") return
      try {
        const result = await this.userLogout()
        this.$message.success(result)
        const return_url = this.$route.fullPath
        this.$router.push({name: "login", query: {return_url}})
      } catch (error) {
        this.$message.error(error)
      }
    }
  },
  computed: {
    /* 用户的ID */
    userId() {
      return this.$store.state.userStore.userInfo.id
    },
    /* 用户的名称 */
    nickName() {
      return this.$store.state.userStore.userInfo.nickName
    }
  },
  mounted() {
    this.$eventBus.$on("clearKeyword", (value) => {
      this.keyword = value
    })
  }
}
</script>

<template>
  <header class="header">
    <!--快捷导航栏-->
    <div class="top">
      <div class="container">
        <div class="loginList">
          <p>尚品汇欢迎您！</p>
          <p v-if="userId">
            <a>{{ nickName }}</a> <a class="register" @click="logoutHandle">退出登录</a>
          </p>
          <p v-else>
            <router-link to="/login">请登录</router-link>
            <router-link to="/register" class="register">免费注册</router-link>
          </p>
        </div>
        <div class="typeList">
          <router-link to="/center">我的订单</router-link>
          <router-link to="/shopcart">我的购物车</router-link>
          <a>我的尚品汇</a>
          <a>尚品汇会员</a>
          <a>企业采购</a>
          <a>关注尚品汇</a>
          <a>合作招商</a>
          <a>商家后台</a>
        </div>
      </div>
    </div>
    <!--搜索区域-->
    <div class="bottom">
      <h1 class="logoArea">
        <router-link to="/" class="logo">
          <img src="../../assets/logo.png">
        </router-link>
      </h1>
      <div class="searchArea">
        <form class="searchForm">
          <input type="text" class="input-error input-xxlarge" placeholder="请输入搜索关键字" v-model="keyword"/>
          <button @click.prevent="onSearch">搜索</button>
        </form>
      </div>
    </div>
  </header>
</template>

<style scoped lang="less">
.header {
  & > .top {
    background-color: #eaeaea;
    height: 30px;
    line-height: 30px;

    .container {
      width: 1200px;
      margin: 0 auto;
      overflow: hidden;

      .loginList {
        float: left;

        p {
          float: left;
          margin-right: 10px;

          .register {
            border-left: 1px solid #b3aeae;
            padding: 0 5px;
            margin-left: 5px;
          }
        }
      }

      .typeList {
        float: right;

        a {
          padding: 0 10px;

          & + a {
            border-left: 1px solid #b3aeae;
          }
        }

      }

    }
  }

  & > .bottom {
    width: 1200px;
    margin: 0 auto;
    overflow: hidden;

    .logoArea {
      float: left;

      .logo {
        img {
          width: 175px;
          margin: 25px 45px;
        }
      }
    }

    .searchArea {
      float: right;
      margin-top: 35px;

      .searchForm {
        overflow: hidden;

        input {
          box-sizing: border-box;
          width: 490px;
          height: 32px;
          padding: 0px 4px;
          border: 2px solid #ea4a36;
          float: left;

          &:focus {
            outline: none;
          }
        }

        button {
          height: 32px;
          width: 68px;
          background-color: #ea4a36;
          border: none;
          color: #fff;
          float: left;
          cursor: pointer;

          &:focus {
            outline: none;
          }
        }
      }
    }
  }
}
</style>
